package com.example.courseorder.controller;


import com.example.courseorder.domain.Course;
import com.example.courseorder.domain.Student;
import com.example.courseorder.domain.User;
import com.example.courseorder.service.CourseService;
import com.example.courseorder.service.LoginService;
import com.example.courseorder.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;


@Controller
public class StudentController {
    Student student=new Student();
    Course course=new Course();
    User temp = new User();
    @RequestMapping("/wel")
    public String welcome() {
        System.out.println("返回初始界面");
        return "welcome";
    }
    @RequestMapping("/logins")
    public String loginForm(Model model) {
        model.addAttribute("users",temp);
        System.out.println(model);
        return "logins";
    }
    @RequestMapping("/register")
    public String registerForm(Model model) {
        model.addAttribute("users",temp);
        System.out.println(model);
        return "register";
    }
    //注册完回到欢迎界面
    @Autowired
    LoginService loginService;
    @RequestMapping("/personRegister")
    public String toForm(@RequestParam("username") String name,
                         @RequestParam("password") String password1,
                         @Valid @ModelAttribute("users") User user,
                         BindingResult result,
                         Model model) {
        System.out.println("注册进入");
        System.out.println(name);
        System.out.println(password1);
        System.out.println(user);
        if(result.hasErrors()) {
            System.out.println("注册信息出错");
            return "register";
        }
        loginService.insertUser(user);
        return "welcome";
    }
    //登录后进入学生信息界面
    @RequestMapping("/personLogin")
    public String informationForm(@RequestParam("username") String name,
                                  @RequestParam("password") String password1,
                                  @Valid @ModelAttribute("users") User user,
                                  BindingResult result,
                                  Model model) {
        student.setUsername(name);
        temp.setUsername(name);
        System.out.println(user.getUsername());
        System.out.println(user.getPassword());
        User user1 =  loginService.getUser(user.getPassword(),user.getUsername());
        System.out.println("用户"+user1);
        if(result.hasErrors()||user1 == null){
            System.out.println("用户名为空");
            return "logins";
        }
        //model.addAttribute("temp", temp);
        model.addAttribute("students",student);
       // System.out.println("s1"+model);
        return "student";
    }
    @Autowired
    @Qualifier("courseService")
    CourseService courseService;
    @PostMapping("/courses")
    public String studentInfo(@RequestParam("username")String username,
                              @RequestParam("name")String name,
                              @RequestParam("sno")String sno,
                              @RequestParam("grade")String grade,
                              @RequestParam("department")String department,
                              @RequestParam("academy")String academy,
                              @Valid @ModelAttribute("students") Student students,
                              BindingResult result,
                              Model model) {
        System.out.println("进来了");
        System.out.println("用户名"+username);
        //System.out.println(students);
        if(result.hasErrors()) {
            System.out.println("输入错误");
            //model.addAttribute("students",student);
            return "student";
        }
        student.setName(name);
        student.setSno(sno);
        student.setGrade(grade);
        student.setDepartment(department);
        student.setAcademy(academy);
        System.out.println(student);
        System.out.println(courseService.findAll());
        model.addAttribute("students",courseService.findAll());
        System.out.println(model);
        return "courseInfo";
    }
    @Autowired
    @Qualifier("studentService")
    StudentService studentService;
    //StudentDao studentDao;
    @PostMapping("/display")
    public String display(Model model,@RequestParam("course")String course,
                          @Valid @ModelAttribute("students") Student students,
                          BindingResult result) {
        student.setCourse(course);
        System.out.println(student);
        studentService.add(student);
        //studentDao.add(student);
        model.addAttribute("students",student);
        System.out.println(model);
        return "display";
    }
    @RequestMapping({"/logout"})
    public String logout(HttpServletRequest request, HttpServletResponse response) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth != null) {//清除认证
            new SecurityContextLogoutHandler().logout(request, response, auth);
        }

        return "welcome";

    }
}
